﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Runtime.CompilerServices;
using System.Diagnostics;

namespace CygwinPackageManager.Common
{
	public class PackageStatusDALC
	{
		public IEnumerable<PackageStatus> GetStatuses()
		{
			return Statuses;
		}

		[MethodImpl(MethodImplOptions.Synchronized)]
		private static IEnumerable<PackageStatus> _GetStatuses()
		{
			List<PackageStatus> statuses = new List<PackageStatus>();

			foreach (PackageStatusIndex statusIndex in Enum.GetValues(typeof(PackageStatusIndex)))
			{
				string name = string.Empty;
				switch (statusIndex)
				{
					case PackageStatusIndex.Available:
						name = "Available";
						break;
					case PackageStatusIndex.InstalledOutdated:
						name = "Installed Outdated";
						break;
					case PackageStatusIndex.InstalledUpdated:
						name = "Installed Updated";
						break;
					case PackageStatusIndex.MarkedForInstallation:
						name = "Marked for Installation";
						break;
					case PackageStatusIndex.MarkedForRemoval:
						name = "Marked for Removal";
						break;
					case PackageStatusIndex.MarkedForUpdate:
						name = "Marked for Update";
						break;
					case PackageStatusIndex.None:
						continue;
					default:
						throw new NotImplementedException();
				}

				statuses.Add(new PackageStatus()
				{
					Name = name,
					PackageStatusID = (int)statusIndex,
				});
			}

			return statuses;
		}

		private static IEnumerable<PackageStatus> _statuses;
		private static IEnumerable<PackageStatus> Statuses
		{
			[MethodImpl(MethodImplOptions.Synchronized)]
			get
			{
				if (_statuses == null)
					_statuses = _GetStatuses();

				return _statuses;
			}
		}
	}
}
